import java.util.*;

public class GetSkyline {

    public List<List<Integer>> getSkyline(int[][] buildings) {
        ArrayList<int[]> list = new ArrayList<>();
        for (int[] building : buildings) {
            int l = building[0];
            int r = building[1];
            int h = building[2];
            list.add(new int[]{l, -h});
            list.add(new int[]{r, h});
        }
        Collections.sort(list, (a, b) -> {
            if (a[0] != b[0]) return a[0] - b[0];
            return a[1] - b[1];
        });
        PriorityQueue<Integer> heap = new PriorityQueue<>((a, b) -> b - a);
        heap.add(0);
        int pre = 0;
        List<List<Integer>> ans = new ArrayList<>();
        for (int[] val : list) {
            int h = val[1];
            int x = val[0];
            if (h < 0) {
                heap.add(-h);
            } else {
                heap.remove(h);
            }
            Integer cur = heap.peek();
            if (cur <= Math.abs(h) && cur != pre) {
                ans.add(Arrays.asList(x, cur));
                pre = cur;
            }
        }
        return ans;
    }
}
